package com.dc.service;

import com.dc.constant.Constants;
import com.dc.constant.MessageConstant;
import com.dc.dao.*;
import com.dc.entity.*;
import com.dc.exception.ServiceException;
import com.dc.service.dto.*;
import com.dc.web.controller.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 单船工序计算服务
 */
@Service
public class WorkflowService {

    @Resource
    private ShipDao shipDao;

    @Resource
    private ShipRepairDao shipRepairDao;

    @Resource
    private WorkflowElectricBoxDao workflowElectricBoxDao;

    @Resource
    private WorkflowShipCutDao workflowShipCutDao;

    @Resource
    private WorkflowShipOldDao workflowShipOldDao;

    @Resource
    private WorkflowShipWeldDao workflowShipWeldDao;

    @Resource
    private WorkflowMachinePipeDao workflowMachinePipeDao;

    @Resource
    private WorkflowTowShipOilDao workflowTowShipOilDao;

    @Resource
    private WorkflowOtherOilShipDao workflowOtherOilShipDao;

    @Resource
    private WorkflowOtherOilCarDao workflowOtherOilCarDao;

    @Resource
    private WorkflowPaintShipDao workflowPaintShipDao;

    @Resource
    private WorkflowPaintHouseDao workflowPaintHouseDao;

    @Resource
    private WorkflowShipSandDao workflowShipSandDao;

    @Resource
    private WorkflowHouseSandDao workflowHouseSandDao;

    @Resource
    private WorkflowWaterCleanDao workflowWaterCleanDao;

    @Resource
    private CalibrationService calibrationService;

    public List getWorkflows(String workflow, Long shipRepairId) {
         switch (workflow) {
            case Constants.WORK_FLOW_ELECTRIC_BOX:
                return calcElectricBox(shipRepairId);
            case Constants.WORK_FLOW_SHIP_CUT:
                return calcShipCut(shipRepairId);
            case Constants.WORK_FLOW_SHIP_OLD:
                return calcShipOld(shipRepairId);
            case Constants.WORK_FLOW_SHIP_WELD:
                return calcShipWeld(shipRepairId);
            case Constants.WORK_FLOW_MACHINE_PIPE:
                return calcMachinePipe(shipRepairId);
            case Constants.WORK_FLOW_TOW_SHIP_OIL:
                return calcTowShipOil(shipRepairId);
            case Constants.WORK_FLOW_OTHER_OIL_SHIP:
                 return calcOtherOilShip(shipRepairId);
            case Constants.WORK_FLOW_OTHER_OIL_CAR:
                 return calcOtherOilCar(shipRepairId);
            case Constants.WORK_FLOW_PAINT_SHIP:
                 return calcPaintShip(shipRepairId);
            case Constants.WORK_FLOW_PAINT_HOUSE:
                 return calcPaintHouse(shipRepairId);
            case Constants.WORK_FLOW_SHIP_SAND:
                 return calcShipSand(shipRepairId);
            case Constants.WORK_FLOW_HOUSE_SAND:
                 return calcHouseSand(shipRepairId);
            case Constants.WORK_FLOW_WATER_CLEAN:
                 return calcWaterClean(shipRepairId);
            default:
                 throw new ServiceException(MessageConstant.ERR_WORK_FLOW_UNKOWN);
        }
    }

    /**
     * 移动电箱的  电能  计算
     * @param shipRepairId
     * @return
     */
    public List<WorkflowElectricBoxDto> calcElectricBox(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowElectricBoxEntity> electricBoxEntityList = workflowElectricBoxDao.findByShipRepairEntity(shipRepairEntity);

        List<WorkflowElectricBoxDto> result = new ArrayList<>();

        String workflow = Constants.WORK_FLOW_ELECTRIC_BOX;

        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Map<String, Double> collect = electricBoxEntityList.stream()
                .collect(Collectors.groupingBy(WorkflowElectricBoxEntity::getRoom, Collectors.summingDouble(WorkflowElectricBoxEntity::getPower)));


//        double sum = electricBoxEntityList.stream().mapToDouble(WorkflowElectricBoxEntity::getPower).sum();

        for(Map.Entry<String, Double> entry : collect.entrySet()) {
            String room = entry.getKey();
            Double power = entry.getValue();
            WorkflowElectricBoxDto dto = new WorkflowElectricBoxDto(workflow, room, power);

            result.add(dto);
        }

        return result;
    }

    /**
     * 0 船体下料 o2 air
     */
    public List<WorkflowShipCutDto> calcShipCut(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowShipCutDto> result = new ArrayList<>();

        String workflow = Constants.WORK_FLOW_SHIP_CUT;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowShipCutEntity> shipCutEntities = workflowShipCutDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationShipCutDto calibrationShipCutDto = calibrationService.calcShipCut();

        shipCutEntities.forEach(t -> {
            WorkflowShipCutController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationShipCutDto);
        });

        Map<String, List<WorkflowShipCutEntity>> collect = shipCutEntities.stream()
                .collect(Collectors.groupingBy(WorkflowShipCutEntity::getRoom));

        for(Map.Entry<String, List<WorkflowShipCutEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowShipCutEntity> valueList = entry.getValue();
            double sumO2 = valueList.stream().mapToDouble(WorkflowShipCutEntity::getO2).sum();
            double sumAir = valueList.stream().mapToDouble(WorkflowShipCutEntity::getAir).sum();
            WorkflowShipCutDto dto = new WorkflowShipCutDto(workflow, room, sumO2, sumAir);

            result.add(dto);
        }

        return result;
    }

    /**
     * 1 船体割旧 天然气 gas  氧气o2
     */
    public List<WorkflowShipOldDto> calcShipOld(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowShipOldDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_SHIP_OLD;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowShipOldEntity> shipOldEntities = workflowShipOldDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationShipOldDto calibrationShipOldDto = calibrationService.calcShipOld();

        shipOldEntities.forEach(t -> {
            WorkflowShipOldController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationShipOldDto);
        });

//        double sumGas = shipOldEntities.stream().mapToDouble(WorkflowShipOldEntity::getGas).sum();
//        double sumO2 = shipOldEntities.stream().mapToDouble(WorkflowShipOldEntity::getO2).sum();

        Map<String, List<WorkflowShipOldEntity>> collect = shipOldEntities.stream()
                .collect(Collectors.groupingBy(WorkflowShipOldEntity::getRoom));



        for(Map.Entry<String, List<WorkflowShipOldEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowShipOldEntity> valueList = entry.getValue();
            double sumO2 = valueList.stream().mapToDouble(WorkflowShipOldEntity::getO2).sum();
            double sumGas = valueList.stream().mapToDouble(WorkflowShipOldEntity::getGas).sum();
            WorkflowShipOldDto dto = new WorkflowShipOldDto(workflow, room, sumGas, sumO2);

            result.add(dto);
        }

        return result;
    }

    /**
     * 2 船体焊接 二氧化碳 co2  电量 power
     */
    public List<WorkflowShipWeldDto> calcShipWeld(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowShipWeldDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_SHIP_WELD;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowShipWeldEntity> shipOldEntities = workflowShipWeldDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationShipWeldDto calibrationShipWeldDto = calibrationService.calcShipWeld();

        shipOldEntities.forEach(t -> {
            WorkflowShipWeldController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationShipWeldDto);
        });

        Map<String, List<WorkflowShipWeldEntity>> collect = shipOldEntities.stream()
                .collect(Collectors.groupingBy(WorkflowShipWeldEntity::getRoom));



        for(Map.Entry<String, List<WorkflowShipWeldEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowShipWeldEntity> valueList = entry.getValue();
            double sumCo2 = valueList.stream().mapToDouble(WorkflowShipWeldEntity::getCo2).sum();
            double sumPower = valueList.stream().mapToDouble(WorkflowShipWeldEntity::getPower).sum();
            WorkflowShipWeldDto dto = new WorkflowShipWeldDto(workflow, room, sumCo2, sumPower);

            result.add(dto);
        }

        return result;
    }

    /**
     * 3 机电管子维修 电量 power
     */
    public List<WorkflowMachinePipeDto> calcMachinePipe(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowMachinePipeDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_MACHINE_PIPE;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowMachinePipeEntity> machinePipeEntities = workflowMachinePipeDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationMachinePipeDto calibrationMachinePipeDto = calibrationService.calcMachinePipe();

        machinePipeEntities.forEach(t -> {
            WorkflowMachinePipeController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationMachinePipeDto);
        });

//        double sumFlPower = machinePipeEntities.stream().mapToDouble(WorkflowMachinePipeEntity::getFlPower).sum();
//        double sumWgPower = machinePipeEntities.stream().mapToDouble(WorkflowMachinePipeEntity::getWgPower).sum();

        Map<String, List<WorkflowMachinePipeEntity>> collect = machinePipeEntities.stream()
                .collect(Collectors.groupingBy(WorkflowMachinePipeEntity::getRoom));



        for(Map.Entry<String, List<WorkflowMachinePipeEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowMachinePipeEntity> valueList = entry.getValue();
            double sumFlPower = valueList.stream().mapToDouble(WorkflowMachinePipeEntity::getFlPower).sum();
            double sumWgPower = valueList.stream().mapToDouble(WorkflowMachinePipeEntity::getWgPower).sum();
            double sum = sumFlPower + sumWgPower;

            WorkflowMachinePipeDto dto = new WorkflowMachinePipeDto(workflow, room, sum);

            result.add(dto);
        }

        return result;
    }

    /**
     * 4 拖船油耗 柴油 oil
     */
    public List<WorkflowTowShipOilDto> calcTowShipOil(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowTowShipOilDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_TOW_SHIP_OIL;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowTowShipOilEntity> towShipOilEntities = workflowTowShipOilDao.findByShipRepairEntity(shipRepairEntity);

//        CalibrationMachinePipeDto calibrationMachinePipeDto = calibrationService.calcMachinePipe();
//
//        machinePipeEntities.forEach(t -> {
//            WorkflowTowShipOilController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationMachinePipeDto);
//        });

        Map<String, Double> collect = towShipOilEntities.stream()
                .collect(Collectors.groupingBy(WorkflowTowShipOilEntity::getRoom, Collectors.summingDouble(WorkflowTowShipOilEntity::getOil)));

//        double sum = electricBoxEntityList.stream().mapToDouble(WorkflowElectricBoxEntity::getPower).sum();



        for(Map.Entry<String, Double> entry : collect.entrySet()) {
            String room = entry.getKey();
            Double oil = entry.getValue();
            WorkflowTowShipOilDto dto = new WorkflowTowShipOilDto(workflow, room, oil);

            result.add(dto);
        }

        return result;
    }


    /**
     * 5-0 其他油耗-单船
     */
    public List<WorkflowOtherOilShipDto> calcOtherOilShip(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowOtherOilShipDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_OTHER_OIL_SHIP;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowOtherOilShipEntity> otherOilShipEntities = workflowOtherOilShipDao.findByShipRepairEntity(shipRepairEntity);

//        CalibrationMachinePipeDto calibrationMachinePipeDto = calibrationService.calcMachinePipe();
//
//        machinePipeEntities.forEach(t -> {
//            WorkflowTowShipOilController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationMachinePipeDto);
//        });

        Map<String, List<WorkflowOtherOilShipEntity>> collect = otherOilShipEntities.stream()
                .collect(Collectors.groupingBy(WorkflowOtherOilShipEntity::getRoom));



        for(Map.Entry<String, List<WorkflowOtherOilShipEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowOtherOilShipEntity> valueList = entry.getValue();
            double sumSendNum = valueList.stream().mapToDouble(WorkflowOtherOilShipEntity::getSendNum).sum();

            WorkflowOtherOilShipDto dto = new WorkflowOtherOilShipDto(workflow, room, sumSendNum);

            result.add(dto);
        }

        return result;
    }

    /**
     * 5-1 其他油耗-高空车
     */
    public List<WorkflowOtherOilCarDto> calcOtherOilCar(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowOtherOilCarDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_OTHER_OIL_CAR;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowOtherOilCarEntity> otherOilCarEntities = workflowOtherOilCarDao.findByShipRepairEntity(shipRepairEntity);

//        CalibrationMachinePipeDto calibrationMachinePipeDto = calibrationService.calcMachinePipe();
//
//        machinePipeEntities.forEach(t -> {
//            WorkflowTowShipOilController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationMachinePipeDto);
//        });

        Map<String, List<WorkflowOtherOilCarEntity>> collect = otherOilCarEntities.stream()
                .collect(Collectors.groupingBy(WorkflowOtherOilCarEntity::getRoom));



        for(Map.Entry<String, List<WorkflowOtherOilCarEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowOtherOilCarEntity> valueList = entry.getValue();
            double sumSendNum = valueList.stream().mapToDouble(WorkflowOtherOilCarEntity::getSendNum).sum();

            WorkflowOtherOilCarDto dto = new WorkflowOtherOilCarDto(workflow, room, sumSendNum);

            result.add(dto);
        }

        return result;
    }

    /**
     * 6-0 涂装喷漆-舱室及船壳喷漆
     */
    public List<WorkflowPaintShipDto> calcPaintShip(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowPaintShipDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_PAINT_SHIP;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowPaintShipEntity> paintShipEntities = workflowPaintShipDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationPaintShipDto calibrationPaintShipDto = calibrationService.calcPaintShip();
//
        paintShipEntities.forEach(t -> {
            WorkflowPaintShipController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationPaintShipDto);
        });

        Map<String, List<WorkflowPaintShipEntity>> collect = paintShipEntities.stream()
                .collect(Collectors.groupingBy(WorkflowPaintShipEntity::getRoom));



        for(Map.Entry<String, List<WorkflowPaintShipEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowPaintShipEntity> valueList = entry.getValue();
            double sumAir = valueList.stream().mapToDouble(WorkflowPaintShipEntity::getAir).sum();

            WorkflowPaintShipDto dto = new WorkflowPaintShipDto(workflow, room, sumAir);

            result.add(dto);
        }

        return result;
    }

    /**
     * 6-1 涂装喷漆-喷漆房分段喷漆
     */
    public List<WorkflowPaintHouseDto> calcPaintHouse(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowPaintHouseDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_PAINT_HOUSE;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowPaintHouseEntity> paintHouseEntities = workflowPaintHouseDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationPaintHouseDto calibrationPaintHouseDto = calibrationService.calcPaintHouse();
//
        paintHouseEntities.forEach(t -> {
            WorkflowPaintHouseController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationPaintHouseDto);
        });

        Map<String, List<WorkflowPaintHouseEntity>> collect = paintHouseEntities.stream()
                .collect(Collectors.groupingBy(WorkflowPaintHouseEntity::getRoom));



        for(Map.Entry<String, List<WorkflowPaintHouseEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowPaintHouseEntity> valueList = entry.getValue();

            double sumPower = valueList.stream().mapToDouble(WorkflowPaintHouseEntity::getPowers).sum();
            double sumGas = valueList.stream().mapToDouble(WorkflowPaintHouseEntity::getGas).sum();
            double sumAir = valueList.stream().mapToDouble(WorkflowPaintHouseEntity::getAir).sum();

            WorkflowPaintHouseDto dto = new WorkflowPaintHouseDto(workflow, room, sumPower, sumGas, sumAir);

            result.add(dto);
        }

        return result;
    }

    /**
     * 7-0 打砂除锈-舱室打砂
     */
    public List<WorkflowShipSandDto> calcShipSand(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowShipSandDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_SHIP_SAND;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowShipSandEntity> shipSandEntities = workflowShipSandDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationShipSandDto calibrationShipSandDto = calibrationService.calcShipSand();
//
        shipSandEntities.forEach(t -> {
            WorkflowShipSandController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationShipSandDto);
        });

        Map<String, List<WorkflowShipSandEntity>> collect = shipSandEntities.stream()
                .collect(Collectors.groupingBy(WorkflowShipSandEntity::getRoom));



        for(Map.Entry<String, List<WorkflowShipSandEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowShipSandEntity> valueList = entry.getValue();

            double sumAir = valueList.stream().mapToDouble(WorkflowShipSandEntity::getAir).sum();

            WorkflowShipSandDto dto = new WorkflowShipSandDto(workflow, room, sumAir);

            result.add(dto);
        }

        return result;
    }

    /**
     * 7-1 打砂除锈-打砂房分段打砂
     */
    public List<WorkflowHouseSandDto> calcHouseSand(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowHouseSandDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_HOUSE_SAND;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowHouseSandEntity> houseSandEntities = workflowHouseSandDao.findByShipRepairEntity(shipRepairEntity);

        CalibrationHouseSandDto calibrationHouseSandDto = calibrationService.calcHouseSand();
//
        houseSandEntities.forEach(t -> {
            WorkflowHouseSandController.dynamicCalc(t, shipEntity, shipRepairEntity, calibrationHouseSandDto);
        });

        Map<String, List<WorkflowHouseSandEntity>> collect = houseSandEntities.stream()
                .collect(Collectors.groupingBy(WorkflowHouseSandEntity::getRoom));



        for(Map.Entry<String, List<WorkflowHouseSandEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowHouseSandEntity> valueList = entry.getValue();

            double sumPower = valueList.stream().mapToDouble(WorkflowHouseSandEntity::getPowers).sum();
            double sumAir = valueList.stream().mapToDouble(WorkflowHouseSandEntity::getAir).sum();

            WorkflowHouseSandDto dto = new WorkflowHouseSandDto(workflow, room, sumPower, sumAir);

            result.add(dto);
        }

        return result;
    }

    /**
     * 8 超水除锈
     */
    public List<WorkflowWaterCleanDto> calcWaterClean(Long shipRepairId) {

        Optional<ShipRepairEntity> optionalShipRepair = shipRepairDao.findById(shipRepairId);
        if(!optionalShipRepair.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_REPAIR_MISSING);
        }
        ShipRepairEntity shipRepairEntity = optionalShipRepair.get();

        List<WorkflowWaterCleanDto> result = new ArrayList<>();
        String workflow = Constants.WORK_FLOW_WATER_CLEAN;
        if(!isContains(shipRepairEntity, workflow)) {
            return result;
        }

        Long shipId = shipRepairEntity.getShipId();
        Optional<ShipEntity> optionalShip = shipDao.findById(shipId);
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity shipEntity = optionalShip.get();

        List<WorkflowWaterCleanEntity> waterCleanEntities = workflowWaterCleanDao.findByShipRepairEntity(shipRepairEntity);

        waterCleanEntities.forEach(t -> {
            WorkflowWaterCleanController.dynamicCalc(t, shipEntity, shipRepairEntity);
        });

        Map<String, List<WorkflowWaterCleanEntity>> collect = waterCleanEntities.stream()
                .collect(Collectors.groupingBy(WorkflowWaterCleanEntity::getRoom));



        for(Map.Entry<String, List<WorkflowWaterCleanEntity>> entry : collect.entrySet()) {
            String room = entry.getKey();
            List<WorkflowWaterCleanEntity> valueList = entry.getValue();

            double sumPower = valueList.stream().mapToDouble(WorkflowWaterCleanEntity::getPowers).sum();

            WorkflowWaterCleanDto dto = new WorkflowWaterCleanDto(workflow, room, sumPower);

            result.add(dto);
        }

        return result;
    }

    private Boolean isContains(ShipRepairEntity entity, String workflow) {
        String workFlows = entity.getWorkFlows();
        String[] split = workFlows.split(",");
        for(String s : split) {
            if(s.equals(workflow)) {
                return true;
            }
        }
        return false;
    }
}
